<!--
 * @Description: 活动列表
 * @Autor: 何兴家
 * @Date: 2020-12-23 16:33:31
 * @LastEditors: 何兴家
 * @LastEditTime: 2020-12-24 16:37:04
-->
<template>
  <div>
    <FilterTable
      ref="table"
      columnsRef="repair_repair_reception_customer_activity"
      height="auto"
      :columns="columns"
      :dataSource="tableDataSource"
      selectionType="single"
      :onRowSelectChange="handleTableRowSelect"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>
    <BaseDialog :visible.sync="showPanel" title="服务活动明细" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <activityDetail :viewRowData="viewRowData" @close="showPanel = false" />
    </BaseDialog>

    <div class="dialogBottmView">
      <el-button @click="handleCancle">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { confirmAction, notifyAction, findFormItem, findTableItem } from '@/utils';

import activityDetail from './activityDetail';

import { queryVehicleActivity } from '@/api/repair/repairReception/customer';

export default {
  props: ['vin', 'selectedAvtivityCodeList'],
  components: { activityDetail },
  mixins: [dictionary],
  data() {
    this.selectedRow = []; // 选中的行项目
    this.viewRowData = {}; // 选中查看的行项
    return {
      columns: this.createTableColumns(),
      showPanel: false,
      tableDataSource: [], // table数据源
      syncTableData: [] // 同步的table数据
    };
  },
  mounted() {
    this.queryTableData();
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          render: props => {
            return (
              <el-button
                type="text"
                onClick={() => {
                  this.handleViewDetail(props.row);
                }}
              >
                详情
              </el-button>
            );
          }
        },
        {
          title: '是否已存在',
          dataIndex: 'isAdded',
          disabled: true,
          editable: true,
          defaultEditable: true,
          editType: 'checkbox',
          editItems: [
            { text: '', trueValue: 10041001 },
            { text: '', falseValue: 10041002 }
          ]
        },
        {
          title: '活动编号',
          dataIndex: 'MA_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '活动名称',
          dataIndex: 'MA_TITLE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        // {
        //   title: '活动类型',
        //   dataIndex: '',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '活动开始日期',
          dataIndex: 'B_DATE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '活动结束日期',
          dataIndex: 'E_DATE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '活动总金额',
          dataIndex: 'LABOUR_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    collapseHandle() {
      this.nextTick(() => {
        this.refs.table.EXECUTE_RESET_HEIGHT();
      });
    },
    // 同步table数据
    handleSyncTableData(list) {
      list.forEach(item => {
        this.selectedAvtivityCodeList.includes(item.MA_NO) && (item.isAdded = 1004001);
      });
      this.syncTableData = list;
    },
    // 行项选中变化
    handleTableRowSelect(list) {
      this.selectedRow = list;
    },
    // 初始化查询数据
    async queryTableData() {
      this.$refs.table.START_LOADING();
      try {
        let res = await queryVehicleActivity({ vin: this.vin });
        this.tableDataSource = res.data.activityInfo;
      } catch (err) {
        console.log(err, '查询服务活动失败');
      } finally {
        this.$refs.table.STOP_LOADING();
      }
    },
    // 点击查看详情
    handleViewDetail(row) {
      this.viewRowData = row;
      this.showPanel = true;
    },
    // 点击取消
    handleCancle() {
      this.$emit('close');
    },
    // 点击确定
    handleConfirm() {
      if (!this.selectedRow.length) return notifyAction('请选择需要带回的活动', 'warning');
      // 带回前处理部分数据
      this.selectedRow[0].labourList.forEach(item => {
        item.ACTIVITY_CODE = this.selectedRow[0].MA_NO;
        item.ASSIGN_LABOUR_HOUR = item.STD_LABOUR_HOUR;
      });
      this.selectedRow[0].partList.forEach(item => {
        item.PART_SALES_PRICE = item.PRICE;
        item.PRICE_RATE = 1;
        item.PART_QUANTITY = item.COUNTS;
        item.ACTIVITY_CODE = this.selectedRow[0].MA_NO;
      });
      this.$emit('onConfirm', this.selectedRow);
    }
  }
};
</script>

<style scoped></style>
